<?php

namespace App\Containers\AppSection\Pledge\Models;

use App\Containers\AppSection\Pledge\Observers\PledgeObserver;
use App\Containers\AppSection\Project\Models\Project;
use App\Containers\AppSection\Type\Models\Type;
use App\Ship\Parents\Models\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
 * Залог проекта
 *
 * @property int $id Id
 * @property int $project_id Проект
 * @property string $property_type Тип Имущества
 * @property int $property_id Id имущества
 * @property string $rating_key Рейтинг
 * @property int $collateral_value Стоимость
 * @property string $description Описание
 * @property Carbon $created_at Время создания
 * @property Carbon $updated_at Время обновления
 *
 * @property Project $project Проект
 * @property Type $rating Рейтинг
 * @property Property|DDU|Other|Movable $property Имущество
 */
class Pledge extends Model
{
    protected $fillable = [
        'project_id',
        'property_type',
        'property_id',
        'rating_key',
        'collateral_value',
        'description',
    ];

    protected $dates = [
        'created_at',
        'updated_at',
    ];

    /**
     * A resource key to be used in the serialized responses.
     */
    protected string $resourceKey = 'Pledge';

    protected static function boot()
    {
        parent::boot();
        self::observe(new PledgeObserver);
    }

    /**
     * Проект
     * @return BelongsTo Project
     */
    public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class, 'project_id', 'id');
    }

    /**
     * Имущество
     * @return MorphTo Property|DDU|Other|Movable
     */
    public function property(): MorphTo
    {
        return $this->morphTo('property');
    }

    /**
     * Рейтинг
     * @return BelongsTo Type
     */
    public function rating(): BelongsTo
    {
        return $this->belongsTo(Type::class, 'rating_key', 'key');
    }
}
